package com.oa.ylms.apps.actions.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.oa.ylms.apps.actions.BasicAction;
import com.oa.ylms.apps.business.file.IFileInfoService;
import com.oa.ylms.apps.business.file.IFileSubmitInfoService;
import com.oa.ylms.apps.business.organization.ISectionInfoService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.file.FileInfoValue;
import com.oa.ylms.apps.entity.file.FileSubmitInfoValue;
import com.oa.ylms.apps.entity.organization.SectionInfoValue;

@SuppressWarnings("serial")
public class FileSubmitInfoAction extends BasicAction {
	private List<FileInfoValue> fileList;
	private List<FileSubmitInfoValue> submitList;
	private List<SectionInfoValue> sectionList;
	private FileInfoValue fileInfo;
	private IFileInfoService fileInfoService;
	private IFileSubmitInfoService submitInfoService;
	private ISectionInfoService sectionService;
	
	private File submitFileInfo;
	private String submitFileInfoContentTpye;
	private String submitFileInfoFileName;
	
	private Integer fileId;
	private String abstracts;
	private String submitTime;
	private Integer sectionId;
	private String searchTime;
	private Integer id;
	private String ids;
	private String str;
	
	private String uploadDir;

	@Override
	public String execute() throws Exception {
		fileList = fileInfoService.qureyFileInfo(IDEDStates.ENABLE);
		return "show";
	}
	
	public String addSubmit() throws Exception {
		String newFileName = null;
		long now = new Date().getTime();
		fileInfo = fileInfoService.queryFileById(fileId);
		uploadDir = "/"+fileInfo.getFileUrl();
		
		String path = ServletActionContext.getServletContext().getRealPath(uploadDir);
		
		File dir = new File(path);
		if(!dir.exists()){
			dir.mkdir();
		}
		
		int index = submitFileInfoFileName.lastIndexOf('.');
		if(index != -1){
			newFileName = submitFileInfoFileName.substring(0,index)+now+submitFileInfoFileName.substring(index);
		}else{
			newFileName = Long.toString(now);
		}
		
		BufferedOutputStream bos = null;
		BufferedInputStream bis = null;
		
		try{
			FileInputStream fis = new FileInputStream(submitFileInfo);
			bis = new BufferedInputStream(fis);
			
			FileOutputStream fos = new FileOutputStream(new File(dir, newFileName));
			bos = new BufferedOutputStream(fos);
			
			byte[] buf = new byte[4096];
			
			int len = -1;
			while((len = bis.read(buf))!= -1){
				bos.write(buf,0,len);
			}
		}finally{
			try{
				if( null != bis)
					bis.close();
			}catch(Exception e){
				e.printStackTrace();
			}
			try{
				if( null != bos)
					bos.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		staffInfo = (StaffInfoValue)session.get("login_staffInfo");
		if(submitInfoService.addFileSubmitInfo(staffInfo, fileId, newFileName, abstracts) >0){
			str = "SUCCESS!";
		}else{
			str = "Error!";
		}
		return SUCCESS;
	}
	
	public String queryByManager() throws Exception{
		if("".equals(searchTime))
			submitTime = null;
		else{
			submitTime = searchTime;
		}
		totalpage = submitInfoService.countTPages(fileId, staffInfo, submitTime, sectionId, rows);
		count = submitInfoService.getTotalNumber();
		if(count%rows==0&&totalpage+1==currpage){
			currpage=totalpage;
		}
		fileList = fileInfoService.qureyFileInfo(IDEDStates.ENABLE);
		sectionList = sectionService.querySectionInfo(null, IDEDStates.ENABLE);
		submitList = submitInfoService.queryFileSubmitInfoWithPage(fileId, staffInfo, submitTime, sectionId, rows, currpage);
		return "queryByManager";
	}
	
	public String queryByPerson() throws Exception{
		staffInfo = (StaffInfoValue)session.get("login_staffInfo");
		if("".equals(searchTime))
			submitTime = null;
		else{
			submitTime = searchTime;
		}
		totalpage = submitInfoService.countTPages(fileId, staffInfo, submitTime, sectionId, rows);
		count = submitInfoService.getTotalNumber();
		if(count%rows==0&&totalpage+1==currpage){
			currpage=totalpage;
		}
		fileList = fileInfoService.qureyFileInfo(IDEDStates.ENABLE);
		sectionList = sectionService.querySectionInfo(null, IDEDStates.ENABLE);
		submitList = submitInfoService.queryFileSubmitInfoWithPage(fileId, staffInfo, submitTime, sectionId, rows, currpage);
		return "queryByPerson";
	}
	
	public String delete() throws Exception{
		if(submitInfoService.deleteFileSubmitInfo(ids)){
			return "delete";
		}
		return "false";
	}
	
	public String deleteByPerson() throws Exception{
		if(submitInfoService.deleteFileSubmitInfo(ids)){
			return "deleteByPerson";
		}
		return "false";
	}

	public void setFileList(List<FileInfoValue> fileList) {
		this.fileList = fileList;
	}

	public List<FileInfoValue> getFileList() {
		return fileList;
	}

	public void setSubmitFileInfo(File submitFileInfo) {
		this.submitFileInfo = submitFileInfo;
	}

	public File getSubmitFileInfo() {
		return submitFileInfo;
	}

	public void setSubmitFileInfoContentTpye(String submitFileInfoContentTpye) {
		this.submitFileInfoContentTpye = submitFileInfoContentTpye;
	}

	public String getSubmitFileInfoContentTpye() {
		return submitFileInfoContentTpye;
	}

	public void setSubmitFileInfoFileName(String submitFileInfoFileName) {
		this.submitFileInfoFileName = submitFileInfoFileName;
	}

	public String getSubmitFileInfoFileName() {
		return submitFileInfoFileName;
	}

	public void setFileInfoService(IFileInfoService fileInfoService) {
		this.fileInfoService = fileInfoService;
	}

	public IFileInfoService getFileInfoService() {
		return fileInfoService;
	}

	public void setSubmitInfoService(IFileSubmitInfoService submitInfoService) {
		this.submitInfoService = submitInfoService;
	}

	public IFileSubmitInfoService getSubmitInfoService() {
		return submitInfoService;
	}

	public void setFileId(Integer fileId) {
		this.fileId = fileId;
	}

	public Integer getFileId() {
		return fileId;
	}

	public void setUploadDir(String uploadDir) {
		this.uploadDir = uploadDir;
	}

	public String getUploadDir() {
		return uploadDir;
	}

	public void setAbstracts(String abstracts) {
		this.abstracts = abstracts;
	}

	public String getAbstracts() {
		return abstracts;
	}

	public void setFileInfo(FileInfoValue fileInfo) {
		this.fileInfo = fileInfo;
	}

	public FileInfoValue getFileInfo() {
		return fileInfo;
	}

	public void setSubmitList(List<FileSubmitInfoValue> submitList) {
		this.submitList = submitList;
	}

	public List<FileSubmitInfoValue> getSubmitList() {
		return submitList;
	}

	public void setSectionList(List<SectionInfoValue> sectionList) {
		this.sectionList = sectionList;
	}

	public List<SectionInfoValue> getSectionList() {
		return sectionList;
	}

	public void setSectionService(ISectionInfoService sectionService) {
		this.sectionService = sectionService;
	}

	public ISectionInfoService getSectionService() {
		return sectionService;
	}

	public String getSubmitTime() {
		return submitTime;
	}

	public void setSubmitTime(String submitTime) {
		this.submitTime = submitTime;
	}

	public Integer getSectionId() {
		return sectionId;
	}

	public void setSectionId(Integer sectionId) {
		this.sectionId = sectionId;
	}

	public String getSearchTime() {
		return searchTime;
	}

	public void setSearchTime(String searchTime) {
		this.searchTime = searchTime;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return id;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getIds() {
		return ids;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public String getStr() {
		return str;
	}
	
}
